from audioop import reverse
from copy import copy
from math import fabs
import os
import shutil
from tokenize import Number
import cv2
from matplotlib.pyplot import pink
import numpy as np
Root_path= 'D:\\project\\python\\datasets\\ccpd_green\\images/'
Save_path = Root_path+'changeNameTrain/'
Cut_path=Root_path+'cutImg/'
class Separete:
    def __init__(self):
        print("init sucess")

    def getPonit(self,names:str)->list:
        fist_spilt=names.split('-')
        point_str=fist_spilt[3]
        second_spilt=point_str.split('_')
        left_up_str,left_down_str,right_up_str,right_down_str=second_spilt[2],second_spilt[1],second_spilt[3],second_spilt[0]
        left_up_x,left_up_y=int(left_up_str.split('&')[0]),int(left_up_str.split('&')[1])
        left_down_x,left_down_y=int(left_down_str.split('&')[0]),int(left_down_str.split('&')[1])
        right_up_x,right_up_y,=int(right_up_str.split('&')[0]),int(right_up_str.split('&')[1])
        right_down_x, right_down_y=int(right_down_str.split('&')[0]),int(right_down_str.split('&')[1])
        return [[left_up_x,left_up_y],[left_down_x,left_down_y],[right_up_x,right_up_y],[right_down_x, right_down_y]]
    
    def getTwoPoint(self,names:str)->list:
        fist_spilt=names.split('-')
        point_str=fist_spilt[2]
        second_spilt=point_str.split('_')
        left_str,right_str=second_spilt[0],second_spilt[1]
        left_x,left_y=left_str.split('&')[0],left_str.split('&')[1]
        right_x,right_y=right_str.split('&')[0],right_str.split('&')[1]
        return [[int(left_x),int(left_y)],[int(right_x),int(right_y)]]

    def getPath(self,path:str):
        file_names=os.listdir(path)
        for file in file_names:
            point_list=self.getPonit(path+file)
            a_x=point_list[0][0]-10
            a_y=point_list[0][1]-10
            b_x=point_list[3][0]+10
            b_y=point_list[3][1]+10
            img = cv2.imread(path+file)
            cropImg = img[a_y:b_y,a_x:b_x]
            cv2.imwrite(Cut_path+file,cropImg)
    
    def find_contour(self,img_gray):
         #find_contour
            middle_line = len(img_gray)//2
            middle_vertical= len(img_gray[0])//2
            
            border_color=0
            find_top = 0
            count = 0
            # find_top
            # print(img_gray.shape,'shape')
            # 59  3  117 10
            for i in reversed(range(middle_line)):
                # print(img_gray[i][middle_vertical],i,middle_vertical)
                # print(img_gray[i][middle_vertical],i)
                if img_gray[i][middle_vertical]>100:
                    count=count+1
                if count>3 and img_gray[i][middle_vertical]<100:
                   # verify left has black and white
                    find_couter=0
                    isWhite=True
                    for _i in reversed(range(middle_vertical)):
                        if isWhite and int(img_gray[find_top][_i]) < 100:
                            isWhite = False
                            find_couter=find_couter+1
                        if isWhite==False and int(img_gray[find_top][_i])>100:
                            isWhite= True
                            find_couter=find_couter+1
                    if find_couter<11:
                        break
                find_top=i
                border_color=img_gray[i][middle_vertical]
            # print(middle_line)
            # find left
            
            stop_posi=0
            for i in reversed(range(middle_vertical)):
                # print(img_gray[find_top][i],i,border_color)
                
                if abs(int(img_gray[find_top][i])-border_color)<30 :
                    border_color=img_gray[find_top][i]
                else:
                    #find up 
                    _find_up=False
                    for _j in range(5):
                        if abs(int(img_gray[find_top-_j][i])-border_color)<30:
                            border_color=img_gray[find_top-_j][i]
                            _find_up=True
                            break
                        
                    if _find_up == False:
                        # find down
                        _find_down = False
                        for _j in range(5):
                            # print(int(img_gray[find_top+_j][i]),border_color,find_top+_j)
                            if abs(int(img_gray[find_top+_j][i])-border_color)<30:
                                border_color=img_gray[find_top+_j][i]
                                _find_down=True
                                break
                        # print(_find_down)
                        if  _find_down == False:
                            stop_posi=i
                            print(img_gray[find_top][i],i,'fail')
                            break
                
            #     for _i in range(middle_vertical):
            cv2.line(img_gray,(middle_vertical,middle_line),(middle_vertical,find_top),(0, 0, 255),thickness=1,lineType=cv2.LINE_AA) 
            cv2.line(img_gray,(middle_vertical,find_top),(stop_posi,find_top),(0, 0, 255),thickness=1,lineType=cv2.LINE_AA) 
    
    def list_add(self,lists)->int:
        total_num = 0
        for i in lists:
            total_num = total_num + i
        return total_num

    def find_average(self,img_gray):
        average_line = len(img_gray)//2
        average_vertical=len(img_gray[2])//2
        background_colors=[]
        background_color=0
        font_color = 0
        list_temp=[]
        total=[]
        top = int(len(img_gray) / 5)
        
        # for i in reversed(range(average_line)):
        #     if i == top:
        #         break
        #     value = img_gray[i][average_vertical]
        #     if value>90:
        #         background_colors.append(value)
        #     else:
        #         list_temp.append(value)
        list_temp = img_gray[average_line-2:average_line+2,average_vertical-2:average_vertical+2]
        for line in list_temp:
            print(line)
        
        # print(list_temp)
        # return
        # background_color = int(self.list_add(background_colors)/len(background_colors))
        # # print(background_color)
        # # print(list_temp,self.list_add(list_temp)/len(list_temp))
        # # font_color = int(self.list_add(list_temp)/len(list_temp))
        # # print(font_color,background_color)
        # vertical_list = []
        # left=0
        # for i in range(average_line):
            
        #     vertical_list.append(img_gray[i][average_vertical])
        # print(vertical_list,len(vertical_list),top)
        # cv2.line(img_gray,(average_vertical,average_line),(average_vertical,top),(0, 0, 255),thickness=1,lineType=cv2.LINE_AA)

        # color_list = []
        # color_length= []
        
        # for vertical in img_gray:
        #     for line in vertical:
        #         if line not in color_list:
        #             color_list.append(line)
        #             color_length.append(1)
        #         else:
        #             index = color_list.index(line)
        #             color_length[index] = color_length[index] + 1
        # temp_list=copy(color_length)
        # temp_list.sort()
        # index = color_length.index(temp_list[len(temp_list)-1])
        # average = color_list[index]
        # for i in range(len(img_gray)):
        #     for j in range(len(img_gray[i])):
        #         if img_gray[i][j]>100:
        #             img_gray[i][j]=255
        #         if img_gray[i][j]<100:
        #             img_gray[i][j]=0
        return img_gray
    def perspective(self,path:str):
        file_names=os.listdir(path)
        
        for file in file_names:
            img = cv2.imread(path+file)
            img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            img_data = copy(img_gray)
            self.find_average(img_data)
            # self.find_contour(img_data)
            # cv2.imshow('cut',img_gray)
            cv2.imshow('data',img_data)
            cv2.waitKey(0)
           


def main():
    s = Separete()
    # if not os.path.exists(Cut_path):
    #     os.mkdir(Cut_path)
    # else:
    #     shutil.rmtree(Cut_path)
    #     os.mkdir(Cut_path)
    # s.getPath(Save_path)
    s.perspective(Cut_path)

if __name__=="__main__":
    main()


